package com.guomao.charStream;

import java.io.*;

public class FileReaderWriterDemo {


    public static void main(String[] args) throws Exception {

//        alt + shift + m
//        Fileread1();

//        使用系统默认的utf-8一次写一个字符
//        fileWrite1();

//        readWriteFile();


        BufferedReader br = new BufferedReader(new FileReader("abc.txt"));

//        System.out.println(br.readLine());
//        System.out.println(br.readLine());
//        System.out.println(br.readLine());

        String line;
        while ((line = br.readLine()) != null) {
//            System.out.print(line);

        }
    }


    public static void readWriteFile() throws Exception {

//        BufferedReader br = new BufferedReader(new FileReader("content.txt"));
        BufferedReader br = new BufferedReader(new FileReader("demo1.mp4"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("demo2.mp4"));
        String line;
        //readLine 可以读取一整行 如果读取到文件末尾 返回null
        while ((line = br.readLine()) != null) {
            //不会写换行符
            bw.write(line);
            bw.newLine();//执行换行操作
//            \r\n
//            \n
        }

        br.close();
        bw.close();
    }


    private static void fileWrite1() throws IOException {

        FileWriter fw = new FileWriter("test.txt");

        fw.write("Hello World");
        fw.write("我的叔叔于勒");

        char[] arr = {'中', '国', '福', '建'};

        fw.write(arr);

        //字符流写数据强制刷新
        fw.flush();
        fw.close();
    }

    private static void fileRead1() throws IOException {
        //使用系统默认的编码表编解码
        //一次读取三个字节 合并数据
        FileReader fr = new FileReader("abc.txt");


        int i = fr.read();//25105

        System.out.println(i);
/*        int i = fr.read();

        System.out.println(i);

        i = fr.read();

        System.out.println(i);*/

/*
        int i;
        while ((i = fr.read()) != -1) {
            System.out.print((char) i);
        }
*/
       /* int i = fr.read();
        System.out.println((char) i);*/

/*        int i;
        while ((i = fr.read()) != -1) {
            System.out.print((char) i);
        }*/
//        char [] chars = new char[128];
//        int i = fr.read(chars);
//        System.out.println(i);//6 代表本次 读取到的字符个数
//        System.out.println(new String(chars,0,i));
    }
}
